package com.shine.utils
{
	import com.shine.manager.StageManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class BitmapUtil
	{
		
		public static function scaleToArea(bmd:BitmapData,width:Number,height:Number,fit:Boolean=false):BitmapData
		{
			var _bmp:Bitmap=new Bitmap(bmd.clone(),"auto",true);
			var _temp_scale1:Number=bmd.width/bmd.height;
			var _temp_scale2:Number=width/height;
			
			if(!fit && _bmp.width<=width && _bmp.height<=height) 
			{
				return bmd.clone();
			}
			
			if(_temp_scale1>_temp_scale2){
				_bmp.scaleX=_bmp.scaleY=Number((width/bmd.width).toFixed(2));
			}else{
				_bmp.scaleX=_bmp.scaleY=Number((height/bmd.height).toFixed(2));
			}
			
			var _bmd:BitmapData=new BitmapData(_bmp.width,_bmp.height);
			var _matr:Matrix = new Matrix();
			_matr.createBox(_bmp.scaleX,_bmp.scaleY,0,_bmp.x,_bmp.y);
			_bmd.draw(_bmp,_matr,null,null,null,true);
			return _bmd;
		}
		
		public static function scaleCutArea(bmd:BitmapData,width:Number,height:Number):BitmapData
		{
			var _bmp:Bitmap=new Bitmap(bmd.clone(),"auto",true);
			var _temp_scale1:Number=_bmp.width/bmd.height;
			var _temp_scale2:Number=width/height;
			
			if(_temp_scale1<_temp_scale2){
				_bmp.scaleX=_bmp.scaleY=Number((width/bmd.width).toFixed(2));
			}else{
				_bmp.scaleX=_bmp.scaleY=Number((height/bmd.height).toFixed(2));
			}
			
			var _bmd:BitmapData=new BitmapData(width,height);
			var _matr:Matrix = new Matrix();
			_matr.createBox(_bmp.scaleX,_bmp.scaleY,0,_bmp.x-(_bmp.width-width)*0.5,_bmp.y-(_bmp.height-height)*0.5);
			_bmd.draw(_bmp,_matr,null,null,null,true);
			return _bmd;
		}
		
		public static function splitFrames(bmd:BitmapData, split_width:int, split_height:int=-1,dx:int=0,dy:int=0):Vector.<BitmapData>
		{
			var temp_height:int = split_height==-1 ? bmd.height : split_height;
			var i:int=0;
			var length:int = (bmd.width / split_width);
			var list:Vector.<BitmapData> = new Vector.<BitmapData>(length, true);
			
			var origin:Point = new Point();
			var sourceRect:Rectangle = new Rectangle();
			
			sourceRect.width = split_width;
			sourceRect.height = temp_height;
			
			while (i < length) {
				sourceRect.x = dx+(i * split_width);
				sourceRect.y = dy;
				list[i] = new BitmapData(split_width, temp_height, true, 0);
				list[i].copyPixels(bmd, sourceRect, origin);
				i++;
			};
			
			return list;
		}
		
		public static function toBitmapData(target:DisplayObject,width:uint=0,height:uint=0,dx:int=0,dy:int=0):BitmapData
		{
			var stage:Stage= StageManager.getStage();
			if(stage){
				var oldQuality:String = stage.quality;
				stage.quality = "BEST";
			}
			var _ct:ColorTransform=target.transform.colorTransform;
			var _matr:Matrix=target.transform.matrix.clone();
			
			var _bitmapData:BitmapData=new BitmapData((width==0 ? target.width : width),(height==0 ? target.height :height),true,0x00000000);
			var _rect:Rectangle=target.getBounds(target);

			var _tx:Number=target.x-(dx==0 ? _rect.x : dx);
			var _ty:Number=target.y-(dy==0 ? _rect.y : dy);
			
			_matr.tx+=_tx;
			_matr.ty+=_ty;
			
			_bitmapData.draw(target,_matr,_ct,null,null,true);
			
			if(stage) stage.quality = oldQuality;
			return _bitmapData;
		}
	}
}